package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DriverInfo;
import com.ruoyi.system.domain.VehicleInfo;
import com.ruoyi.system.mapper.DriverInfoMapper;
import com.ruoyi.system.mapper.VehicleInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.VehicleUsageMapper;
import com.ruoyi.system.domain.VehicleUsage;
import com.ruoyi.system.service.IVehicleUsageService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 车辆使用记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-05
 */
@Service
public class VehicleUsageServiceImpl implements IVehicleUsageService 
{
    @Autowired
    private VehicleUsageMapper vehicleUsageMapper;

    @Autowired
    private DriverInfoMapper driverInfoMapper;
    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;
    /**
     * 查询车辆使用记录
     * 
     * @param id 车辆使用记录ID
     * @return 车辆使用记录
     */
    @Override
    public VehicleUsage selectVehicleUsageById(Long id)
    {
        VehicleUsage vehicleUsage = vehicleUsageMapper.selectVehicleUsageById(id);
        String whereToWhere = vehicleUsage.getWhereToWhere();
        if(StringUtils.isNotEmpty(whereToWhere))
        {
            String[] whereArr = whereToWhere.split(" 到 ");
            vehicleUsage.setFromWhere(whereArr[0].trim());
            vehicleUsage.setToWhere(whereArr[1].trim());

        }
        return vehicleUsage;
    }

    /**
     * 查询车辆使用记录列表
     * 
     * @param vehicleUsage 车辆使用记录
     * @return 车辆使用记录
     */
    @Override
    public List<VehicleUsage> selectVehicleUsageList(VehicleUsage vehicleUsage)
    {
        return vehicleUsageMapper.selectVehicleUsageList(vehicleUsage);
    }

    /**
     * 新增车辆使用记录
     * 
     * @param vehicleUsage 车辆使用记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertVehicleUsage(VehicleUsage vehicleUsage)
    {
        Date startTime = vehicleUsage.getStartTime();
        Date endTime = vehicleUsage.getEndTime();

        if(startTime.after(endTime))
        {
            throw new BusinessException("使用开始时间不能大于使用结束时间!");
        }

        String fromWhere = vehicleUsage.getFromWhere();
        String toWhere = vehicleUsage.getToWhere();

        if(StringUtils.isEmpty(fromWhere))
        {
            throw new BusinessException("请填写起点!");
        }
        if(StringUtils.isEmpty(toWhere))
        {
            throw new BusinessException("请填写终点!");
        }
        String whereToWhere = fromWhere+" 到 "+toWhere;
        vehicleUsage.setWhereToWhere(whereToWhere);
        //驾驶员编号
        String driverNumber = vehicleUsage.getDriverNumber();
        DriverInfo driverInfoQuery = new DriverInfo();
        driverInfoQuery.setDriverNumber(driverNumber);
        DriverInfo selectDriverInfo = driverInfoMapper.selectDriverInfo(driverInfoQuery);
        if(Objects.isNull(selectDriverInfo))
        {
            throw new BusinessException("该驾驶员编号在驾驶员列表信息中不存在，请你重新输入!");
        }

        //车牌号
        String vehicleNumber = vehicleUsage.getVehicleNumber();
        VehicleInfo vehicleInfoQuery = new VehicleInfo();
        vehicleInfoQuery.setVehicleNumber(vehicleNumber);
        VehicleInfo selectVehicleInfo = vehicleInfoMapper.selectVehicleInfo(vehicleInfoQuery);
        if(Objects.isNull(selectVehicleInfo))
        {
            throw new BusinessException("该车牌号在车辆信息列表中不存在，请你重新输入!");
        }

        //将该车辆的状态改为使用中
        VehicleInfo updateVehicleInfo = new VehicleInfo();
        updateVehicleInfo.setId(selectVehicleInfo.getId());
        updateVehicleInfo.setStatus(2L);
        vehicleInfoMapper.updateVehicleInfo(updateVehicleInfo);

        //设置基本信息
        vehicleUsage.setStatus(1L);//新增完就默认正在使用
        vehicleUsage.setIsDeleted(0L);
        vehicleUsage.setCreateBy(ShiroUtils.getLoginName());
        vehicleUsage.setCreateTime(new Date());
        vehicleUsage.setUpdateBy(ShiroUtils.getLoginName());
        vehicleUsage.setUpdateTime(new Date());
        return vehicleUsageMapper.insertVehicleUsage(vehicleUsage);
    }

    /**
     * 修改车辆使用记录
     * 
     * @param vehicleUsage 车辆使用记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int updateVehicleUsage(VehicleUsage vehicleUsage)
    {
        String fromWhere = vehicleUsage.getFromWhere();
        String toWhere = vehicleUsage.getToWhere();
        if(StringUtils.isEmpty(fromWhere))
        {
            throw new BusinessException("请填写起点!");
        }
        if(StringUtils.isEmpty(toWhere))
        {
            throw new BusinessException("请填写终点!");
        }
        String whereToWhere = fromWhere+" 到 "+toWhere;
        vehicleUsage.setWhereToWhere(whereToWhere);
        //驾驶员编号
        String driverNumber = vehicleUsage.getDriverNumber();
        DriverInfo driverInfoQuery = new DriverInfo();
        driverInfoQuery.setDriverNumber(driverNumber);
        DriverInfo selectDriverInfo = driverInfoMapper.selectDriverInfo(driverInfoQuery);
        if(Objects.isNull(selectDriverInfo))
        {
            throw new BusinessException("该驾驶员编号在驾驶员列表信息中不存在，请你重新输入!");
        }

        //车牌号
        String vehicleNumber = vehicleUsage.getVehicleNumber();
        VehicleInfo vehicleInfoQuery = new VehicleInfo();
        vehicleInfoQuery.setVehicleNumber(vehicleNumber);
        VehicleInfo selectVehicleInfo = vehicleInfoMapper.selectVehicleInfo(vehicleInfoQuery);
        if(Objects.isNull(selectVehicleInfo))
        {
            throw new BusinessException("该车牌号在车辆信息列表中不存在，请你重新输入!");
        }
        vehicleUsage.setUpdateBy(ShiroUtils.getLoginName());
        vehicleUsage.setUpdateTime(new Date());
        return vehicleUsageMapper.updateVehicleUsage(vehicleUsage);
    }

    /**
     * 删除车辆使用记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteVehicleUsageByIds(String ids)
    {
        return vehicleUsageMapper.deleteVehicleUsageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除车辆使用记录信息
     * 
     * @param id 车辆使用记录ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteVehicleUsageById(Long id)
    {
        return vehicleUsageMapper.deleteVehicleUsageById(id);
    }

    /**
     * 归还车辆
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int retrunCar(VehicleUsage vehicleUsage) {

        Long id = vehicleUsage.getId();
        VehicleUsage selectedVehicleUsage = vehicleUsageMapper.selectVehicleUsageById(id);
        if(Objects.isNull(selectedVehicleUsage))
        {
            throw new BusinessException("查出此车辆使用记录!");
        }
        if(Objects.equals(selectedVehicleUsage.getStatus(),2L))
        {
            throw new BusinessException("该车辆已归还");
        }
        VehicleInfo queryVehicleInfo = new VehicleInfo();
        queryVehicleInfo.setVehicleNumber(selectedVehicleUsage.getVehicleNumber());
        VehicleInfo selectVehicleInfo = vehicleInfoMapper.selectVehicleInfo(queryVehicleInfo);
        if(Objects.isNull(selectVehicleInfo))
        {
            throw new BusinessException("查出此车辆!");
        }

        //将该车辆的状态改为待使用
        VehicleInfo updateVehicleInfo = new VehicleInfo();
        updateVehicleInfo.setId(selectVehicleInfo.getId());
        updateVehicleInfo.setStatus(1L);
        vehicleInfoMapper.updateVehicleInfo(updateVehicleInfo);

        selectedVehicleUsage.setUpdateBy(ShiroUtils.getLoginName());
        selectedVehicleUsage.setUpdateTime(new Date());
        selectedVehicleUsage.setStatus(2L);//车辆使用记录改为已归还
        vehicleUsageMapper.updateVehicleUsage(selectedVehicleUsage);
        return 1;
    }
}
